package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.user.TokenCreateRequest;
import com.example.fireassistant_server.dto.wechat.WechatUserDto;
import com.example.fireassistant_server.entity.Token;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.entity.WeChatUser;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tokens")
public class TokenController {

    UserService userService;

    private WechatUserService wechatUserService;
    //获取token
    @PostMapping
    public Token create(@Validated @RequestBody TokenCreateRequest tokenCreateRequest) {
        Token token = new Token();
        token.setTokenKey(userService.createToken(tokenCreateRequest));
        return token;
    }

    // 微信用户创建token
    @PostMapping("/wechat")
    public Token createWeChat(@RequestHeader(value = "X-WX-OPENID") String openId) {
        //WechatUserDto weChatUser = wechatUserService.getCurrentUser(openId); //Modify at 10-31
        WechatUserDto weChatUser = wechatUserService.getCurrentUser("123456");
        User user = weChatUser.getUser();
        TokenCreateRequest tokenCreateRequest = new TokenCreateRequest();
        tokenCreateRequest.setUsername(user.getUsername());
        tokenCreateRequest.setPassword(user.getPassword());
        Token token = new Token();
        token.setTokenKey(userService.createToken(tokenCreateRequest));
        return token;
    }


    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }
}
